package com.petRescueCenter.demo.voluntary.entity.DTO;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.jpa.domain.Specification;

import com.petRescueCenter.demo.common.enumPackage.LockedEmun;
import com.petRescueCenter.demo.voluntary.entity.Voluntary;

public class VoluntaryQueryDTO {
	private Long id;
	private String title;
	// 是否截止
	private String isEnd;
	// 是否已完成
	private String isCompleted;
	private String states;
	private String search;

	public String getSearch() {
		return search;
	}

	public void setSearch(String search) {
		this.search = search;
	}

	public Long getId() {
		return id;
	}

	public String getTitle() {
		return title;
	}

	public String getIsEnd() {
		return isEnd;
	}

	public String getIsCompleted() {
		return isCompleted;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public void setIsEnd(String isEnd) {
		this.isEnd = isEnd;
	}

	public String getStates() {
		return states;
	}

	public void setStates(String states) {
		this.states = states;
	}

	public void setIsCompleted(String isCompleted) {
		this.isCompleted = isCompleted;
	}

	@SuppressWarnings({ "serial" })
	public static Specification<Voluntary> getWhereClause(final VoluntaryQueryDTO voluntaryQueryDTO) {
		return new Specification<Voluntary>() {
			@Override
			public Predicate toPredicate(Root<Voluntary> root, CriteriaQuery<?> query,
					CriteriaBuilder criteriaBuilder) {
				List<Predicate> predicate = new ArrayList<>();
				if (null != voluntaryQueryDTO.getId()) {
					predicate.add(criteriaBuilder.equal(root.get("id").as(Long.class), voluntaryQueryDTO.getId()));
				}
				if (StringUtils.isNotBlank(voluntaryQueryDTO.getIsEnd())) {
					if (voluntaryQueryDTO.getIsEnd().equals("0")) {
						predicate.add(
								criteriaBuilder.greaterThanOrEqualTo(root.get("endTime").as(Date.class), new Date()));
					} else {
						predicate
								.add(criteriaBuilder.lessThanOrEqualTo(root.get("endTime").as(Date.class), new Date()));
					}
				}

				if (StringUtils.isNotBlank(voluntaryQueryDTO.getIsCompleted())) {
					if (voluntaryQueryDTO.getIsCompleted().equals("0")) {
						predicate.add(criteriaBuilder.greaterThanOrEqualTo(root.get("activityTimeEnd").as(Date.class),
								new Date()));
					} else if (voluntaryQueryDTO.getIsCompleted().equals("2")) {
						predicate.add(criteriaBuilder.equal(root.get("locked").as(LockedEmun.class), LockedEmun.YES));
					} else {
						predicate.add(criteriaBuilder.lessThanOrEqualTo(root.get("activityTimeEnd").as(Date.class),
								new Date()));
					}
				}
				if (StringUtils.isNotBlank(voluntaryQueryDTO.getStates())) {
					if (voluntaryQueryDTO.getStates().equals("YES")) {
						predicate.add(criteriaBuilder.equal(root.get("state").as(LockedEmun.class), LockedEmun.YES));
					}
					if (voluntaryQueryDTO.getStates().equals("NO")) {
						predicate.add(criteriaBuilder.equal(root.get("state").as(LockedEmun.class), LockedEmun.NO));
					}
				}
				Predicate[] pre = new Predicate[predicate.size()];
				return query.where(predicate.toArray(pre)).getRestriction();
			}
		};
	}
}
